import { Card, message, Table } from 'antd';
import React, { Component } from 'react';
import { connect } from 'dva';
import SearchForm from '../../components/AutoForm/SearchForm';
import EditForm from '../../components/AutoForm/EditForm';

@connect(({ loading, finance }) => ({
  tableLoading: loading.effects['finance/page'],
  formLoading: loading.effects['finance/gathering'],
  tableData: finance.pageList,
  pagination: finance.pagination,
  columns: finance.columns,
  selectionIds: finance.selectionIds,
  selectedRows: finance.selectedRows,
  searchValues: finance.searchValues,
  searchFields: finance.searchFields,
  invoiceModal: finance.invoiceModal,
  invoiceForm: finance.invoiceForm,
  invoiceRows: finance.invoiceRows,
}))
export default class NestTable extends Component {
  componentDidMount() {
    this.handleSearch();
  }

  rowClassName = (record, index) => {
    if (record.status === 'SPLIT') {
      return 'orderParent';
    }
  };

  handleSearch = (flag) => {
    const { searchValues } = this.props;
    const { dispatch, pagination } = this.props;
    let pageParam = { pageSize: pagination.pageSize, current: flag==='btn' ? 1 : pagination.current };
    let searchParam = {};
    Object.keys(searchValues).map(key => {
      searchParam[key] = searchValues[key]['value'];
    });
    const param = {
      ...searchParam,
      ...pageParam,
    };
    dispatch({
      type: 'finance/page',
      payload: param,
    });
  };

  handleSearchChange = fields => {
    const { dispatch } = this.props;
    dispatch({
      type: 'finance/handleSearchChange',
      payload: fields,
    });
  };

  onChange = (selectedRowKeys, selectedRows) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'finance/handleSelection',
      payload: { selectedRowKeys, selectedRows },
    });
  };

  onMoney = () => {
    const { dispatch, selectedRows } = this.props;
    if (selectedRows.length !== 1) {
      return message.error('请选一条记录！');
    }

    dispatch({
      type: 'finance/initInvice',
      payload: { fields: selectedRows[0] },
    });
    dispatch({
      type: 'finance/handleInvoiceModal',
      payload: true,
    });
  };

  handleModalVisible = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'finance/handleInvoiceModal',
      payload: false,
    });
  };

  handleModalChange = fields => {
    const { dispatch } = this.props;
    dispatch({
      type: 'finance/handleInvoiceChange',
      payload: { fields },
    });
  };

  handleInvoiceSub = fields => {
    const { dispatch, selectedRows } = this.props;
    dispatch({
      type: 'finance/gathering',
      payload: {
        ...fields,
        id: selectedRows[0].id,
      },
    }).then(() => {
      this.handleSearch();
      this.handleModalVisible();
    });
  };

  /*  onTicket = () => {
    const { dispatch, selectedRows } = this.props;
    if (!selectedRows.length) {
      return message.error('请选要开票的数据！');
    }
    selectedRows.forEach(row => {
      if (row.status !== 'GATHERING') {
        return message.error('状态异常！');
      }
    });
    dispatch({
      type: 'finance/checkInvoice',
      payload: selectedRows,
    })
  };*/

  render() {
    const {
      columns,
      pagination,
      tableLoading,
      tableData,
      dispatch,
      searchValues,
      searchFields,
      formLoading,
      invoiceModal,
      invoiceForm,
      invoiceRows,
    } = this.props;
    pagination.onChange = (page, pageSize) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'finance/page',
        payload: {
          ...searchParam,
          current: page,
          pageSize,
        },
      });
    };
    pagination.onShowSizeChange = (current, size) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'finance/page',
        payload: {
          ...searchParam,
          current: current,
          pageSize: size,
        },
      });
    };

    const rowSelection = {
      type: 'checkbox',
      selections: this.props.selectedRows,
      selectedRowKeys: this.props.selectionIds,
      onChange: this.onChange,
    };

    return (
      <Card bodyStyle={{ padding: 0 }}>
        <SearchForm
          header="开票&收款->操作"
          moneyText="收款"
          layout="inline"
          onFieldsChange={this.handleSearchChange}
          onSubmit={() => {this.handleSearch('btn')}}
          isMoney={true}
          onMoney={this.onMoney}
          fields={searchFields}
          fieldValue={searchValues}
        />
        <Table
          loading={tableLoading}
          columns={columns}
          rowSelection={rowSelection}
          dataSource={tableData}
          size="small"
          bordered
          rowKey="id"
          scroll={{ x: 'max-content' }}
          rowClassName={this.rowClassName}
          pagination={pagination}
        />
        <EditForm
          title="收款"
          rows={invoiceRows}
          fieldValue={invoiceForm}
          onFieldsChange={this.handleModalChange}
          onCancel={this.handleModalVisible}
          onOk={this.handleInvoiceSub}
          confirmLoading={formLoading}
          visible={invoiceModal}
        />
      </Card>
    );
  }
}
